package gov.kqqzyyy.monkey.clinicscreen.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.Toast;

import gov.kqqzyyy.monkey.clinicscreen.ui.MainActivity;

/**
 * Created by Monkey on 2017/10/21.
 * 更新接收器
 */

public class UpdateReceiver extends BroadcastReceiver {

    private final Context mContext;

    public UpdateReceiver(Context context) {
        mContext = context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int code = intent.getIntExtra("updatecode", -1);
        Intent broadIntent = new Intent("toUpdateService");
        switch (code) {
            case 0x11://系统版本最新
                ((MainActivity) mContext).doGetServerTime_New();
                ((MainActivity) mContext).doSetVersion();
                ((MainActivity) mContext).doFlushData(1);
                break;
            case 0x12://检测到新版本
                int newVerCode = intent.getIntExtra("newVerCode", -1);
                String newVerName = intent.getStringExtra("newVerName");
                String updateInfo = intent.getStringExtra("updateInfo");
                ((MainActivity) mContext).showUpdateDialog(newVerCode, newVerName, updateInfo);
                break;
            case 0x13://更新文件下载完成
                ((MainActivity) mContext).cancelProgressDialog();
                Toast.makeText(mContext, "下载完成", Toast.LENGTH_SHORT).show();

                broadIntent.putExtra("toservice", 0x13);
                mContext.sendBroadcast(broadIntent);//发送安装通知

                break;
            case 0x14://下载文件不存在
                ((MainActivity) mContext).cancelProgressDialog();
                Toast.makeText(mContext, "下载文件不存在！", Toast.LENGTH_SHORT).show();
                break;
            case 0x15://下载文件超时
                ((MainActivity) mContext).cancelProgressDialog();
                Toast.makeText(mContext, "下载超时！", Toast.LENGTH_SHORT).show();
                break;
            case 0x17:
                Toast.makeText(mContext, "网络未连接！", Toast.LENGTH_SHORT).show();
                break;
            case 0x18://获取更新数据失败
                Toast.makeText(mContext, "获取更新数据失败！", Toast.LENGTH_SHORT).show();
                new CountDownTimer(5000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        ((MainActivity) mContext).doStartService();
                    }
                }.start();
                break;
            case 0x20://获取配置数据完成
                ((MainActivity) mContext).setWarnList();
                break;
        }
    }
}
